package com.smart.village.big.screen.controller.change;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.smart.village.big.screen.domain.AjaxResult;
import com.smart.village.big.screen.service.ChangeGovernanceService;
import com.smart.village.big.screen.service.GovernanceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.logging.Logger;

import static com.smart.village.big.screen.domain.AjaxResult.error;
import static com.smart.village.big.screen.domain.AjaxResult.success;

/**
 * 新修改的-基层治理数据接口
 */
@Tag(name = "新修改-基层治理数据接口")
@RestController
@RequestMapping("/change/governance")
public class ChangeGovernanceController {
    Logger logger = Logger.getLogger(ChangeGovernanceController.class.getName());

    @Autowired
    private ChangeGovernanceService changeGovernanceService;

    @Operation(summary = "村社治理-特色产业-列表")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 必传"),
            @Parameter(name = "tab", description = "tab名称 【特色种植、定制农业板块、我在北极有块田】"),
    })
    @GetMapping("/getTeSeChanYeList")
    @ResponseBody
    public AjaxResult getTeSeChanYeList(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false) String villageNumber,
            @RequestParam String tab,
            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize
    ) {
        try {
            return AjaxResult.success(changeGovernanceService.getTeSeChanYeList(townNumber, villageNumber, tab, pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    @Operation(summary = "村社治理-特色产业-详情")
    @Parameters({
            @Parameter(name = "tab", description = "tab名称 【特色种植、定制农业板块、我在北极有块田】"),
            @Parameter(name = "naturalResourceSerialNumber", description = "地块编码 必传"),
    })
    @GetMapping("/getTeSeChanYeDetail")
    @ResponseBody
    public AjaxResult getTeSeChanYeDetail(
            @RequestParam String tab,
            @RequestParam String naturalResourceSerialNumber
    ) {
        try {
            return AjaxResult.success(changeGovernanceService.getTeSeChanYeDetail(tab,naturalResourceSerialNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }


    @Operation(summary = "村社治理-地图-图例")
    @GetMapping("/getCunSheZhiLiMapToolLips")
    @ResponseBody
    public AjaxResult getCunSheZhiLiMapToolLips(
            @RequestParam String parentSerialNumber
    ) {
        try {
            return AjaxResult.success(changeGovernanceService.getCunSheZhiLiMapToolLips(parentSerialNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    @Operation(summary = "村社治理-地图")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 必传"),
            @Parameter(name = "parentTagSerialNumber", description = "父蓝牌标签 必传"),
            @Parameter(name = "bluePlateTagSerialNumber", description = "蓝牌标签 必传"),
    })
    @GetMapping("/getCunSheZhiLiMap")
    @ResponseBody
    public AjaxResult getCunSheZhiLiMap(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false) String villageNumber,
            @RequestParam(required = false) String bluePlateTypeSerialNumber

    ) {
        try {
            return AjaxResult.success(changeGovernanceService.getCunSheZhiLiMap(townNumber, villageNumber, bluePlateTypeSerialNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    @Operation(summary = "村社治理-地图-点击详情")
    @Parameters({

            @Parameter(name = "bluePlateSerialNumber", description = "蓝牌编码 必传"),
    })
    @GetMapping("/getCunSheZhiLiMapDetail")
    @ResponseBody
    public AjaxResult getCunSheZhiLiMapDetail(
            @RequestParam String bluePlateSerialNumber
    ) {
        try {
            return AjaxResult.success(changeGovernanceService.getCunSheZhiLiMapDetail(bluePlateSerialNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }


    @Operation(summary = "数智治理 数字门牌 环形图 获取类型下拉")
    @GetMapping("/getDigitalDoorplateStaticsTagList")
    @ResponseBody
    public AjaxResult getDigitalDoorplateStaticsTagList(
    ) {
        try {
            return success(changeGovernanceService.getDigitalDoorplateStaticsTagList());
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }


    @Operation(summary = "数智治理 数字门牌 环形图")
    @Parameters({

            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传"),
            @Parameter(name = "tagSerialNumber", description = "类型"),
    })
    @GetMapping("/getDigitalDoorplateStatics")
    @ResponseBody
    public AjaxResult getDigitalDoorplateStatics(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber,
            @RequestParam String tagSerialNumber
    ) {
        try {
            return success(changeGovernanceService.getDigitalDoorplateStatics(townNumber, villageNumber, tagSerialNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }


    @Operation(summary = "获取大屏 综合治理 边防补助")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传"),
    })
    @GetMapping("/getPatrolCheckIn")
    @ResponseBody
    public AjaxResult getPatrolCheckIn(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber
    ) {
        try {
            return success(changeGovernanceService.getPatrolCheckIn(townNumber, villageNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }


    @Operation(summary = "数智治理 乡村基础数据")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传")
    })
    @GetMapping("/getVillageBaseInformation")
    @ResponseBody
    public AjaxResult getVillageBaseInformation(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber
    ) {
        try {
            return success(changeGovernanceService.getVillageBaseInformation(townNumber, villageNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }
    /**
     * 数智治理 根据 村镇 获取网格坐标列表
     *
     * @return
     */
    @Operation(summary = "数智治理 根据 村镇 获取网格坐标列表")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传")
    })
    @GetMapping("/getGridInformationCoordsByVillage")
    @ResponseBody
    public AjaxResult getGridInformationCoordsByVillage(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber) {
        try {
            JSONObject resultJson = changeGovernanceService.getGridInformationCoordsByVillage(townNumber, villageNumber);
            return success(resultJson);
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 数智治理 根据 村镇 获取网格 图例 按照事件数量排序
     *
     * @return
     */
    @Operation(summary = "数智治理 根据 村镇 获取网格 图例 按照事件数量排序")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传")
    })
    @GetMapping("/getGridInformationToolLipByVillage")
    @ResponseBody
    public AjaxResult getGridInformationToolLipByVillage(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber) {
        try {
            List<JSONObject> resultJson = changeGovernanceService.getGridInformationToolLipByVillage(townNumber, villageNumber);
            return success(resultJson);
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 数智治理 网格坐标点击 查询网格员信息
     *
     * @return
     */
    @Operation(summary = "数智治理 网格坐标点击 查询网格员信息")
    @Parameters({
            @Parameter(name = "gridSerialNumber", description = "网格编码 "),
            @Parameter(name = "role", description = "角色 默认传2 专职网格员（网格长） "),
    })
    @GetMapping("/getGridMemberByGridSerialNumber")
    @ResponseBody
    public AjaxResult getGridMemberByGridSerialNumber(
            @RequestParam(required = false) String gridSerialNumber,
            @RequestParam(required = false) String role
      ) {
        try {
            return success(changeGovernanceService.getGridMemberByGridSerialNumber(gridSerialNumber,role));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 数智治理 根据网格长的身份证号，获取派给他的任务
     *
     * @return
     */
    @Operation(summary = "数智治理 根据网格长的身份证号，获取派给他的任务")
    @Parameters({
            @Parameter(name = "iDCardNumber", description = "网格长 身份证号 "),
    })
    @GetMapping("/getGridEventListByExecReceiver")
    @ResponseBody
    public AjaxResult getGridEventListByExecReceiver(
            @RequestParam String iDCardNumber,
            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize

    ) {
        try {
            return success(changeGovernanceService.getGridEventListByExecReceiver(iDCardNumber,pageNum,pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }
    /**
     * 获取大屏 综合治理 网格概况
     *
     * @return
     */
    @Operation(summary = "获取大屏 综合治理 事件详情")
    @Parameters({
            @Parameter(name = "complaintId", description = "事件id 必传"),
    })
    @GetMapping("/getComplaintsDetail")
    @ResponseBody
    public AjaxResult getComplaintsDetail(
            @RequestParam String complaintId
    ) {
        try {
            JSONObject resultJson = changeGovernanceService.getComplaintsDetail(complaintId);
            return success(resultJson);
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }
    @Operation(summary = "获取大屏 综合治理 网格概况")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传"),
    })
    @GetMapping("/getComprehensiveGridManageStatic")
    @ResponseBody
    public AjaxResult getComprehensiveGridManageStatic(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber) {
        try {
            JSONObject resultJson = changeGovernanceService.getComprehensiveGridManageStatic(townNumber, villageNumber);
            return success(resultJson);
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }


    @Operation(summary = "数智治理 综合治理 巡查走访情况")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传")
    })
    @GetMapping("/getInspectionVisitSituation")
    @ResponseBody
    public AjaxResult getInspectionVisitSituation(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber
    ) {
        try {
            return success(changeGovernanceService.getInspectionVisitSituation(townNumber, villageNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }


    @Operation(summary = "数智治理 综合治理 巡查走访情况-列表")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传"),
            @Parameter(name = "type", description = "类型 必传【关注对象，巡逻员，走访率，事件处理】")
    })
    @GetMapping("/getInspectionVisitSituationList")
    @ResponseBody
    public AjaxResult getInspectionVisitSituationList(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber,
            @RequestParam String type,
            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize
    ) {
        try {
            return success(changeGovernanceService.getInspectionVisitSituationList(townNumber, villageNumber, type, pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 网格巡查任务子任务-列表
     *
     * @return
     */
    @Operation(summary = "网格巡查任务子任务-列表")
    @GetMapping("getPatrolCheckInSubTaskList")
    @ResponseBody
    public AjaxResult getPatrolCheckInSubTaskList(
            @RequestParam @Parameter(description = "主任务号") String taskPlanSerialNumber,
            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize) {
        {

            try {
                if (StringUtils.isBlank(taskPlanSerialNumber)) {
                    return error("主任务号不能为空");
                }
                if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                    pageNum = 1;
                }
                if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                    pageSize = 10;
                }
                return success(
                        changeGovernanceService.
                                getPatrolCheckInSubTaskList(taskPlanSerialNumber, pageNum, pageSize));
            } catch (Exception e) {
                e.printStackTrace();
                logger.severe(e.getMessage());
                return error("程序错误请联系系统管理员");
            }
        }
    }
    /**
     * 网格巡查结果-详情
     *
     * @return
     */
    @Operation(summary = "网格巡查结果-详情")
    @GetMapping("getPatrolResultDetailGridList")
    @ResponseBody
    public AjaxResult getPatrolResultDetailGridList(
            @RequestParam @Parameter(description = "任务编号") String resultMainSerialNumber
    ) {
        try {
            return success(
                    changeGovernanceService.
                            getPatrolResultDetailGridList(resultMainSerialNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }

    }

    @Operation(summary = "村社治理-地图-重点人员")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 必传"),
            @Parameter(name = "personalTagSerialNumber", description = "个人标签编码 必传")
    })
    @GetMapping("/getCunSheZhiLiImportantPersonMap")
    @ResponseBody
    public AjaxResult getCunSheZhiLiImportantPersonMap(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false) String villageNumber,
            @RequestParam(required = false) String personalTagSerialNumber

    ) {
        try {
            return AjaxResult.success(changeGovernanceService.getCunSheZhiLiImportantPersonMap(townNumber, villageNumber,personalTagSerialNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    @Operation(summary = "村社治理-地图-重点人员-图例")
    @Parameters({
    })
    @GetMapping("/getCunSheZhiLiImportantPersonMapToolLips")
    @ResponseBody
    public AjaxResult getCunSheZhiLiImportantPersonMapToolLips(

    ) {
        try {
            return AjaxResult.success(changeGovernanceService.getCunSheZhiLiImportantPersonMapToolLips());
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }



    @Operation(summary = "数智治理 村民共治 第一部分")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传")
    })
    @GetMapping("/getVillagerCoGovernance")
    @ResponseBody
    public AjaxResult getVillagerCoGovernance(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber
    ) {
        try {
            return success(changeGovernanceService.getVillagerCoGovernance(townNumber, villageNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }


    @Operation(summary = "数智治理 村民共治 第二部分")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传"),
            @Parameter(name = "searchType", description = "搜索类型"),
    })
    @GetMapping("/getVillagerCoGovernance2")
    @ResponseBody
    public AjaxResult getVillagerCoGovernance2(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber,
            @RequestParam String searchType
    ) {
        try {
            List<JSONObject> villagerCoGovernance2 = changeGovernanceService.getVillagerCoGovernance2(townNumber, villageNumber, searchType);
            for (int i = 0; i < villagerCoGovernance2.size(); i++) {
                villagerCoGovernance2.get(i).put("rank", i + 1);
            }
            return success(villagerCoGovernance2);
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }


    @Operation(summary = "数智治理 人居环境 第一部分")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传")
    })
    @GetMapping("/getEnvironment1")
    @ResponseBody
    public AjaxResult getEnvironment1(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber
    ) {
        try {
            return success(changeGovernanceService.getEnvironment1(townNumber, villageNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }


    @Operation(summary = "数智治理 人居环境 第一部分  详情")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传"),
            @Parameter(name = "type", description = "类型 非必传 对应页面上的文字 【空气质量】")
    })
    @GetMapping("/getEnvironment1Detail")
    @ResponseBody
    public AjaxResult getEnvironment1Detail(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber,
            @RequestParam(required = false, defaultValue = "") String type
    ) {
        try {
            return success(changeGovernanceService.getEnvironment1Detail(townNumber, villageNumber, type));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }


    @Operation(summary = "数智治理 人居环境 第二部分")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传"),
            @Parameter(name = "status", description = "状态 非必传【待指派、处理中、已处理、拒绝】")
    })
    @GetMapping("/getEnvironment2")
    @ResponseBody
    public AjaxResult getEnvironment2(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber,
            @RequestParam(required = false, defaultValue = "") String status,
            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize
    ) {
        try {
            return success(changeGovernanceService.getEnvironment2(townNumber, villageNumber, status, pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }
}
